package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.InspectionItemsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/checkitem")
public class InspectionItemsController {

    @Autowired
    InspectionItemsService inspectionItemsService;

    /**
     * 查询所有检查项
     *
     * @return
     */
    @GetMapping("/findAll")
    public Result getAllInspectionItems() {
        log.info("查询所有检查项");
        List<CheckItem> checkItems = inspectionItemsService.findAll();
        Result successResult = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);
        return successResult;
    }

    /**
     * 分页查询检查项
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult getInspectionItemsPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("分页查询检查项");
        return inspectionItemsService.findPage(queryPageBean);
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam("checkgroupId") Integer checkgroupId){
        List<Integer> list = inspectionItemsService.findCheckItemIdsByCheckGroupId(checkgroupId);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
    }

    /**
     * 添加检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    public Result addInspectionItems(@RequestBody CheckItem checkItem) {
        log.info("添加检查项{}",checkItem);
        inspectionItemsService.addInspectionItems(checkItem);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id删除检查项
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result deleteInspectionItems(@RequestParam("id") Integer id) {
        log.info("删除检查项{}",id);
        inspectionItemsService.deleteInspectionItems(id);
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id查询检查项
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        log.info("查询检查项{}",id);
        CheckItem checkItem = inspectionItemsService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 编辑检查项
     *
     * @param checkItem
     * @return
     */
    @PostMapping("/edit")
    public Result editInspectionItems(@RequestBody CheckItem checkItem) {
        log.info("编辑检查项{}",checkItem);
        inspectionItemsService.editInspectionItems(checkItem);
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }


}
